<template>
<div>
  <!-- 面包屑导航 -->
  <el-breadcrumb separator-class="el-icon-arrow-right" style="margin-bottom: 20px;">
    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item :to="{ path: '/website/config'}">网站管理</el-breadcrumb-item>
    <el-breadcrumb-item>日志信息</el-breadcrumb-item>
  </el-breadcrumb>
  <!-- 条件查询   -->
  <el-form :inline="true" :model="query" size="mini" >
    <el-form-item label="操作用户:">
      <el-input v-model.trim="query.userName"></el-input>
    </el-form-item>
    <el-form-item label="操作模块:">
      <el-select v-model="query.module" clearable filterable style="width:120px;">
        <el-option v-for="item in moduleList" :key="item.id" :label="item.value" :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="操作类型:">
      <el-select v-model="query.operationType" clearable filterable style="width:120px;">
        <el-option v-for="item in operationTypeList" :key="item.id" :label="item.value" :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="选择开始日期:">
      <el-date-picker v-model="query.startTime" type="date">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="选择结束日期:">
      <el-date-picker v-model="query.endTime" type="date">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="响应结果:">
      <el-select v-model="query.result" clearable filterable style="width:120px;">
        <el-option v-for="item in resultList" :key="item.id" :label="item.value" :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button icon="el-icon-search" type="primary" @click="fetchData">查询</el-button>
      <el-button icon="el-icon-refresh" @click="reload">重置</el-button>
    </el-form-item>
  </el-form>
  <!--  列表数据  -->
  <el-table :data="list" border style="width: 100%">
    <el-table-column align="center" label="序号" width="60" type="index"></el-table-column>
    <el-table-column align="center" prop="userName" label="操作用户" width="200"></el-table-column>
    <el-table-column align="center" prop="module" label="操作模块" width="150">
      <template slot-scope="scope">
        <el-tag type="primary" >{{scope.row.module}}</el-tag>
      </template>
    </el-table-column>
    <el-table-column align="center" prop="operationType" label="操作类型" width="150">
      <template slot-scope="scope">
        <el-tag type="info" >{{scope.row.operationType}}</el-tag>
      </template>
    </el-table-column>
    <el-table-column align="center" prop="description" label="描述" width="180" ></el-table-column>
    <el-table-column align="center" prop="time" label="耗时(毫秒)" width="120" ></el-table-column>
    <el-table-column align="center" prop="ipAddr" label="ip" width="180" ></el-table-column>
    <el-table-column align="center" prop="createTime" label="操作时间" width="180">
      <template slot-scope="scope">
        <!-- 格式化日期 -->
        {{getFormat(scope.row.createTime)}}
      </template>
    </el-table-column>
    <el-table-column align="center" prop="result" label="响应结果" width="150">
      <template slot-scope="scope">
        <el-tag type="success" v-if="scope.row.result === '成功'">{{scope.row.result}}</el-tag>
        <el-tag type="danger" v-if="scope.row.result === '失败'">{{scope.row.result}}</el-tag>
      </template>
    </el-table-column>
    <el-table-column  align="center" label="操作">
      <template slot-scope="scope">
        <el-button icon="el-icon-document-delete" type="danger" size="mini" @click="handleDel(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <!-- 分页 -->
  <div class="block" style="margin-top: 10px;">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="query.current"
      :page-size="query.size"
      background
      layout="total, prev, pager, next"
      :total="total">
    </el-pagination>
  </div>
</div>
</template>

<script>
import {dateFormat} from "@/utils/date";
import logApi from '@/api/log';
export default {
  data() {
    return {
      list: [],
      query: {
        current: 1,
        size: 10,
        module: '',
        operationType: '',
        userName: '',
        startTime: '',
        endTime: '',
        result: ''
      },
      total: '',
      moduleList: [
        {id: 1, value: '系统'},
        {id: 2, value: '博客'},
        {id: 3, value: '音乐'},
        {id: 4, value: '三方'},
        {id: 5, value: '娱乐'}
      ],
      operationTypeList: [
        {id: 1, value: '新增'},
        {id: 2, value: '更新'},
        {id: 3, value: '删除'},
        {id: 4, value: '上传'},
        {id: 5, value: '下载'},
      ],
      resultList: [
        {id: 1, value: '成功'},
        {id: 2, value: '失败'}
      ]
    }
  },

  created() {
    this.fetchData();
  },

  methods: {

    fetchData() {
      logApi.list(this.query).then(response => {
        if (response.code === 200) {
          this.list = response.data.list;
          this.total = response.data.total;
        }
      })
    },

    // val 是切换之后的每页显示多少条
    handleSizeChange(val) {
      this.query.size = val;
      this.fetchData();
    },

    // 当页码改变后触发此方法 val是当前点击（或输入）的页码
    handleCurrentChange(val) {
      this.query.current = val;
      this.fetchData();
    },

    // 重置
    reload() {
      this.query.userName = '';
      this.query.module = '';
      this.query.operationType = '';
      this.query.startTime = '';
      this.query.endTime = '';
      this.query.result = '';
      this.fetchData();
    },

    // 导出excel
    exportExcel() {

    },

    // 删除
    handleDel(id) {
      this.$confirm('确认删除这条记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 发送删除请求
        logApi.delete(id).then(response => {
          if (response.code === 200) {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 1500
            });
          } else if (response.code === 306) {
            this.$notify.error({
              title: '错误',
              message: '您没有操作权限，请联系管理员',
              duration: 1500
            });
          } else {
            this.$notify.error({
              title: '错误',
              message: '系统异常，请稍后再试',
              duration: 1500
            });
          }
          // 刷新列表
          this.fetchData();
        })
      }).catch(() => {
        // 取消删除
      });
    },

    // 格式化日期
    getFormat(date){
      return dateFormat(date);
    },
  }
}
</script>

<style scoped>

</style>
